﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;

namespace Hangout.Client.Controls.User
{
	public partial class UserListBox : UserControl
	{

        public delegate void UserProfileSelectedHandler(AccountService.UserProfile profile);

        public event UserProfileSelectedHandler UserProfileSelected;

		public UserListBox()
		{
			// Required to initialize variables
			InitializeComponent();
		}

        public List<AccountService.UserProfile> users;

        public List<AccountService.UserProfile> Users
        {
            get
            {
                try
                {
                    return users;
                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                    return new List<AccountService.UserProfile>();
                }
            }
            set
            {
                try
                {
                    users = value;
                    if (users.Count > 0)
                    {
                        UsersLB.DataContext = null;
                        UsersLB.ItemsSource = null;
                        UsersLB.DataContext = users;
                        UsersLB.ItemsSource = users;
                        UsersLB.Visibility = System.Windows.Visibility.Visible;
                        NoLBL.Visibility = System.Windows.Visibility.Collapsed;
                    }
                    else
                    {
                        UsersLB.Visibility = System.Windows.Visibility.Collapsed;
                        NoLBL.Visibility = System.Windows.Visibility.Visible;
                    }
                }
                catch (Exception ex)
                {
                    Core.Exceptions.ExceptionReporting.Report(ex);
                    MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
                }
                
            }
        }

        private void UsersLB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (UsersLB.SelectedItem != null)
                {
                    if (UserProfileSelected != null)
                    {
                        UserProfileSelected((AccountService.UserProfile)UsersLB.SelectedItem);
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Exceptions.ExceptionReporting.Report(ex);
                MessageBox.Show(ErrorText.GenericErrorText, ErrorText.GenericErrorCaption, MessageBoxButton.OK);
            }
        }


	}
}